<?php
/**
 * 【促销活动】接口控制器
 * 
 */
class promotionAction extends apiAction {
	
	public function index(){
		$page	= I('page', 1);	// 要显示第几页数据
		$num	= I('num', 20);	// 每页显示多少条数据

		$data = D(MODULE_NAME)->field(array('id','theme'))->where(array('stauts'=>1))->limit($num)->page($page)->select();
		$this->ajaxReturn(200, L('success'), $data);
	}



    /**
     * 创建方案
     * 
     */ 
    public function create(){
        $this->display();
    }

    
    public function actCate(){
        $data = D('promotion')->create($_POST);      
        $data['salerTotalPay'] = $data['salerPay']*$data['salerNum'];
        $mater = $gift = array();
        foreach($_POST['mater'] as $k=>$v){
            foreach($v as $kk=>$vv){
                $mater[$kk][$k] = $vv;
                $mater[$kk]['addUserId'] = $this->user['id'];
                $mater[$kk]['addTime'] = time();
                if(isset($mater[$kk]['materialsSignNum']) && isset($mater[$kk]['materialsSign'])){
                    $mater[$kk]['materialsCountMoney'] = $mater[$kk]['materialsSignNum'] * $mater[$kk]['materialsSign'];
                }                   
            }
        }
        
        
        
        foreach($_POST['gift'] as $k=>$v){
            foreach($v as $kk=>$vv){
                $gift[$kk][$k] = $vv;
                $gift[$kk]['addUserId'] = $this->user['id'];
                $mater[$kk]['addTime'] = time();
                if(isset($gift[$kk]['giftSign']) && isset($gift[$kk]['giftSignNum'])){
                    $gift[$kk]['giftCountMoney'] = $gift[$kk]['giftSign'] * $gift[$kk]['giftSignNum'];
                }                 
            }
        }
        $data['budget'] = 0;
        foreach($mater as $kk=>$vv){
            $data['budget'] += $vv['materialsCountMoney'];
        }
        foreach($gift as $kk=>$vv){
            $data['budget'] += $vv['giftCountMoney'];
        }
        $data['budget'] +=$data['salerTotalPay'];
        $data['startTime'] = strtotime($data['startTime']);
        $data['endTime'] = strtotime($data['endTime']);
        $insertId = D('promotion')->add($data);
        if($insertId>0){
            sort($gift);
            sort($mater);  
            foreach($mater as $kk=>$vv){
                $mater[$kk]['pid'] = $insertId;
            }
            foreach($gift as $kk=>$vv){
                $gift[$kk]['pid'] = $insertId;
            }
            $res  = D('promotion_gift')->addAll($gift) ;
            $res &= D('promotion_materials')->addAll($mater) ;
        }
        if($res){
            redirect('index.php?g=api&m=promotion&a=detail&id='.$insertId, 0);
        }else{
            redirect('index.php?g=api&m=promotion&a=detail&id='.$insertId, 0);
        }
    }

	/**
	 * 促销活动详情
	 */
	public function detail(){
		$id = I('id');
		
		$field = array(
			'p.*',
			//'s.name' => 'storeName',
            //'r.name'=>'microAreasName',
			//'c.name'=>'cityName',
		);
		
		$main = D(MODULE_NAME)->field($field)->alias('p')
				->join(C('DB_PREFIX')."store AS s ON s.id = p.storeId")
				->join(C('DB_PREFIX')."region AS r ON r.id = s.regionId")
				->join(C('DB_PREFIX')."region AS c ON c.id = r.pid")
				->where(array('p.id'=>$id))->find();
		$main['forecastingRatio'] = $main['budget']/(1200*$main['forecastingSales']);
        $main['forecastingRatio'] = number_format($main['forecastingRatio'],'2','.','').'%';
        if($main['actualSales']>0){
    		$main['actualRatio'] = $main['cost']/(1200*$main['actualSales']);
          
            $main['actualRatio'] = number_format($main['actualRatio'],'2','.','').'%';
        }
                
		$materials = D('PromotionMaterials')->where(array('pid'=>$id))->select();
		$gift = D('PromotionGift')->where(array('pid'=>$id))->select();
		
		$this->assign('materials', $materials);
		$this->assign('gift', $gift);
		$this->assign('main', $main);
		
		$this->display();
	}
	
    
    
    
     /**
     * 活动结果录入页面
     * url: /index.php?g=api&m=promotion&a=inputResult&id=35
     */ 
    public function inputResult(){
        $id = I('id');
		$materials = D('PromotionMaterials')->where(array('pid'=>$id))->select();
		$gift = D('PromotionGift')->where(array('pid'=>$id))->select();
        $main = D(MODULE_NAME)->where(array('id'=>$id))->find();
        $this->assign('main', $main);
		$this->assign('materials', $materials);
		$this->assign('gift', $gift);        
        $this->assign('id',$id);
        $this->display();
    }
    
    public function actInputResult(){
        
        $data = D('promotion')->create($_POST);      
       
        $mater = $gift = array();
        foreach($_POST['mater'] as $k=>$v){
     
                $mater[$k] = $v;
                $mater[$k]['inputUserId'] = $this->user['id'];
                $mater[$k]['inputTime'] = time();
                if(isset($mater[$k]['materialsUseNum']) && isset($mater[$k]['materialsSign'])){
                    $mater[$k]['materialsUsePrice'] = $mater[$k]['materialsUseNum'] * $mater[$k]['materialsSign'];
                }                   
       
        }
        foreach($_POST['gift'] as $k=>$v){
            $gift[$k] = $v;
            $gift[$k]['inputUserId'] = $this->user['id'];
            $gift[$k]['inputTime'] = time();
            if(isset($gift[$k]['giftSign']) && isset($gift[$k]['giftUseNum'])){
                $gift[$k]['giftUsePrice'] = $gift[$k]['giftSign'] * $gift[$k]['giftUseNum'];
            }                 
        }
       // print_r($_POST);
        $data['cost'] = 0;
        foreach($mater as $kk=>$vv){
            $data['cost'] += $vv['materialsUsePrice'];
        }
        foreach($gift as $kk=>$vv){
            $data['cost'] += $vv['giftUsePrice'];
        }
        $data['cost'] = array('exp','salerTotalPay+'.$data['cost']); 
        $res = D('promotion')->where(array('id'=>$data['id']))->save($data);
        //D('promotion')->getLastSql();
        if($res){
            foreach($gift as $k=>$v){
                D('promotion_gift')->where(array('id'=>$k))->save($v);
                //echo D('promotion_gift')->getLastSql();
            }
            foreach($mater as $k=>$v){
                D('promotion_materials')->where(array('id'=>$k))->save($v);
                //echo D('promotion_materials')->getLastSql();
            }
        }
        redirect('index.php?g=api&m=promotion&a=detail&id='.$data['id'], 0);    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
	/**
	 * 更新
	 */
	public function update(){
		$id = I('id');
		
		D(MODULE_NAME)->where(array('id'=>$id))->save($_POST);
		redirect('index.php?g=api&m=promotion&a=detail&id='.$id, 0);
	}
	
	/**
	 * 物料填写界面
	 */
	public function materials(){
		$this->display();
	}
	
	/**
	 * 保存物料信息
	 */
	public function materialsSave(){
		$_POST['addUserId'] = $this->user['id'];
		
		if(D('PromotionMaterials')->add($_POST)){
			$this->ajaxReturn(200, L('success'));
		}else{
			$this->ajaxReturn(300, L('error'));
		}
		
	}
	
	/**
	 * 礼品填写界面
	 */
	public function gift(){
		$this->display();
	}
	
	/**
	 * 保存礼品信息
	 */
	public function giftSave(){
		$_POST['addUserId'] = $this->user['id'];
		
		if(D('PromotionGift')->add($_POST)){
			$this->ajaxReturn(200, L('success'));
		}else{
			$this->ajaxReturn(300, L('error'));
		}
		
	}
    
   
     
     
	
	
}
/* End of file promotionAction.class.php */